<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>生命周期</title>
</head>
<body>
    <div id="root"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        // https://source.unsplash.com/500x400/?car
        class App extends React.Component {
            state = {
                keyword: ''
            }

            render(){
                return <div className="container">
                        <button k="city" onClick={this.changeKeyword}>城市</button>
                        <button k="animal" onClick={this.changeKeyword}>动物</button>
                        <button k="car" onClick={this.changeKeyword}>汽车</button>
                        <hr />
                        <Image keyword={this.state.keyword} />
                    </div>
            }

            changeKeyword = (e) => {
                this.setState({
                    keyword: e.target.getAttribute('k')
                })
            }
        }


        class Image extends React.Component {
            //4. 更新
            render(){
                console.log('4-组件更新中. - render')
                //接收关键字的属性
                let {keyword} = this.props;
                return <img src={"https://source.unsplash.com/500x400/?" + keyword} alt="" />
            }
            //1. 将要接收 props   will 将要  receive 接收  props 属性
            componentWillReceiveProps(){
                console.log('1-组件将要接收 props 属性 - componentWillReceiveProps')
            }

            //2. 组件应该要更新
            shouldComponentUpdate(){
                console.log('2-组件应该要更新了 - shouldComponentUpdate');
                return true;
            }

            //3. 组件将要更新
            componentWillUpdate(){
                console.log('3-组件将要更新 - componentWillUpdate');
            }

            //5. 组件完成更新
            componentDidUpdate(){
                console.log('5-组件完成更新 - componentDidUpdate');
            }
        }

        ReactDOM.render(<App />, document.querySelector('#root'));
    </script>
</body>
</html>